import androidHelper from "../../gameComon/scripts/model/androidHelper";
import util from "../../gameComon/scripts/model/util";
import Common from "../Common/Common";
import DataStorage from "./DataStorage";
import ResourcesManager from "./ResourcesManager";

const DataManager = {
    /**游戏数据 */
    GameConfig: {},
    /**当前关卡Id */
    currLevel: 1,
    /**当前关卡数据 */
    currLevelConfig: null,
    /**游戏后台加载 */
    isAppGame: false,

    init() {
        DataManager._isJosnLoaded = false;
    },

    //加载配置数据
    loadComfigDatas(callback) {
        if (this._isJosnLoaded) {
            callback();
            return;
        }
        var jsonArr = ["DinosaurConfig", "DinosaurLevel"];

        var resMgr = ResourcesManager;
        resMgr.loadJson("DinosaurConfig", (data, jsonName) => { DataManager.GameConfig[jsonName] = data; removeJsonitem(jsonName); });
        resMgr.loadJson("DinosaurLevel", (data, jsonName) => { DataManager.GameConfig[jsonName] = data; removeJsonitem(jsonName); });
        // resMgr.loadAudios("res", (data, pathName) => { DataManager.GameConfig[pathName+"Audios"] = Common.arraytoJson(data,"name"); removeJsonitem(pathName); });
        // resMgr.loadAtlas("res",(data,pathName)=>{ DataManager.GameConfig[pathName+"Atlas"] = Common.arraytoJson(data,"name") ,removeJsonitem(pathName); });
        function wait() {
            if (jsonArr.length == 0 && DataManager.isAppGame) {
                callback();
                clearInterval(timer)
                DataManager._isJosnLoaded = true;

            }
        }
        var timer = setInterval(wait, 10);

        function removeJsonitem(item) {
            var index = jsonArr.indexOf(item);
            if (index >= 0) {
                jsonArr.splice(index, 1);
            }
        }
    },

    /**
  * 
  * @param {*} type 获取当前类型装扮配置
  */
    getCurrLevelConfig(type, level) {

        DataManager.currLevelConfig = null;
        // for (let i = 0; i < DataManager.GameConfig[type].length; i++) {
        //     if (DataManager.GameConfig[type][i].id == DataStorage.localData.musicArr[level - 1]) {
        //         DataManager.currLevelConfig = DataManager.GameConfig[type][i];
        //     }
        // }
        DataManager.currLevelConfig = DataManager.GameConfig[type][level];
        return DataManager.currLevelConfig;
    },


    
    adInit() {
        if (appGame.platform == "chuanyinGame") {
            // console.log("adInit+++++++++++++++++++");
            try {
                window.adInit({
                    banner_id: "ca-app-pub-2476175026271293/2335696097",
                    interstitial_id: "ca-app-pub-2476175026271293/9364138877",
                    reward_id: "ca-app-pub-2476175026271293/4522875735",
                    debug_mode: false,
                    //为了适配旧版本的平台版本
                    banner_location: 2,
                    //需要显示banner广告时需要传这个字段
                    custom_banner_location: {
                        //充满屏幕
                        width: -1,
                        //居底
                        gravity: 80,
                        //距离底部50dip
                        margin_bottom: 50
                    },
                    test_device_id: "1713EF848C80930814B50AC748A2FDEC",
                    success(res) {
                        console.log("adInit success = " + res);
                    }
                });
            } catch (error) {
                console.error(error);
            }
        }
    },



    //激励视频广告
    showRewardedVideo(successCallBack, failedCallback) {
        if (appGame.platform == "chuanyinGame") {
            try {
                window.adShow({
                    ad_type: 3,
                    success(res) {//返回值为10001时，代表展示成功，返回值为10002时，代表用户看完了激励广告。
                        // console.log("adShow success =激励视频广告 " + res);
                        if (res == 10001) {
                            // console.log("展示成功");
                        } else if (res == 10002) {
                            if (successCallBack) successCallBack();
                        } else {
                            if (failedCallback) failedCallback();
                        }
                    },
                    fail(res) {
                        // console.log("adShow fail = 激励视频广告" + res);
                        if (failedCallback) failedCallback();
                        appGame.emitter.emit(consts.LOCAL_EVENT_POPUP_LOADTIP, {
                            content: "No ads temporarily. Please try again later!"
                        });
                    }
                });
            } catch (error) {
                appGame.emitter.emit(consts.LOCAL_EVENT_POPUP_LOADTIP, {
                    content: "No ads temporarily. Please try again later!"
                });
            }
        }else if (appGame.platform == "fh" || appGame.platform == "googleAD") {
            
            util.playGoogleAdVideo(function (resultCode) {
                // console.log("视频播放状态：",resultCode);
                if (resultCode == 1) {
                    // cc.log("播放成功")
                    if (successCallBack) successCallBack();
                } else {
                    // cc.log("播放失败")
                    if (failedCallback) failedCallback();
                }
            }.bind(this))
        }else{

            androidHelper.showRewardedVideo(function (result) {
                // console.log("激励视频结果", result);
                if (result == "1") {
                    // console.log("激励视频成功回调")
                    if (successCallBack) successCallBack();
                } else {
                    // console.log("激励视频失败回调")
                    if (failedCallback) failedCallback();
                }
            }.bind(this));
        }
       
    },
    /**隐藏banner */
    hideBannerAd() {
        if (appGame.platform == "chuanyinGame") {

            try {
                window.adClose({
                    ad_type: 1,
                    success(res) {
                        // console.log("adClose success =隐藏banner " + res);
                    },
                    fail(res) {
                        // console.log("adClose fail =隐藏banner " + res);
                    }
                });
            } catch (e) {

            }
        } else {
            androidHelper.hideBannerAd();
        }
    },
    /**展示banner */
    showBannerAd() {
        if (appGame.platform == "chuanyinGame") {
            try {
                window.adShow({
                    ad_type: 1,
                    success(res) {//返回值为10001时，代表展示成功，返回值为10002时，代表用户看完了激励广告。
                        // console.log("adShow success =展示banner " + res);
                    },
                    fail(res) {
                        // console.log("adShow fail =展示banner " + res);
                    }
                });

            } catch (error) {

            }

        } else {
            androidHelper.showBannerAd();
        }
    },
    /**展示插屏视频广告 */
    showInterVideo(successCallBack, failedCallback) {
        androidHelper.showInterVideo(function (result) {
            if (result == "1") {
                successCallBack && successCallBack();
            } else {
                if (failedCallback) failedCallback();
            }
        }.bind(this))
    },
    showInterAdTime:new Date().getTime(),
    /**展示插屏广告 */
    showInterAd() {
        if (appGame.platform == "chuanyinGame") {
            try {
                if (DataManager.showInterAdTime) {
                    let now = new Date().getTime();
                    let interval = now - DataManager.showInterAdTime;
                    console.log("间隔时间", interval);
                    if (interval < 50 * 1000) return;
                } 
                DataManager.showInterAdTime = new Date().getTime();
                console.log("展示插屏广告 adShow");
                
                window.adShow({
                    ad_type: 2,
                    success(res) {//返回值为10001时，代表展示成功，返回值为10002时，代表用户看完了激励广告。
                        // console.log("adShow success =展示插屏广告 " + res);
                    },
                    fail(res) {
                        // console.log("adShow fail =展示插屏广告 " + res);
                    }
                });

            } catch (error) {

            }
            
        }else{
            // console.log("展示插屏广告");
            androidHelper.showInterAd();
        }
    },

    /**自定义事件打点 */
    firebaseEvent(data) {
        androidHelper.firebaseEvent(data);
    },
    /**获取系统语言 */
    getLanguage() {
        return androidHelper.getLanguage();
    }
}
export default DataManager;